<?php
namespace Foam\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\EventManager\EventManagerInterface;
use Zend\View\Model\ViewModel;
use Zend\View\Model\JsonModel;

class SousFamilleController extends AbstractActionController
{
    protected $familleTable;
    protected $ssfamilleTable;
    
    public function setEventManager(EventManagerInterface $events)
    {
        parent::setEventManager($events);
 
        $controller = $this;
        $events->attach('dispatch', function ($e) use ($controller) {
            $controller->layout()->setTemplate('layout/foam');
        }, 100);
 
        return $this;
    }
    
    public function getFamilleTable()
    {
        if (!$this->familleTable) {
            $sm = $this->getServiceLocator();
            $this->familleTable = $sm->get('Foam\Model\FamilleTable');
        }
        return $this->familleTable;
    }
    
    public function getSousFamilleTable()
    {
        if (!$this->ssfamilleTable) {
            $sm = $this->getServiceLocator();
            $this->ssfamilleTable = $sm->get('Foam\Model\SousFamilleTable');
        }
        return $this->ssfamilleTable;
    }
    
    public function indexAction()
    {
        return new ViewModel(array(
            'ssfamilles' => $this->getSousFamilleTable()->getAll(),
        ));
    }
    
    public function savenewAction()
    {
        $request = $this->getRequest();
        if ($request->isPost()) {
            $id_ssfamille = '';
            $libelle = ($request->getPost()->libelle)?$request->getPost()->libelle:'';
            $description = ($request->getPost()->description)?$request->getPost()->description:'';
            $id_famille = ($request->getPost()->id_famille)?$request->getPost()->id_famille:0;
            $couleur_fond = ($request->getPost()->couleur_fond)?$request->getPost()->couleur_fond:'';
            $couleur_bordure = ($request->getPost()->couleur_bordure)?$request->getPost()->couleur_bordure:'';
        }
        
        $this->getSousFamilleTable()->saveNew($id_ssfamille,$libelle,$description,$id_famille,$couleur_fond,$couleur_bordure);

        $viewModel = new ViewModel(array(
            'message' => 'Nouvelle sous-famille enregistrée',
        ));

        $viewModel->setTerminal(true);
        
        return $viewModel;
    }
    
    public function updateAction()
    {
        $request = $this->getRequest();
        
        if ($request->isPost()) {
            
            $pk = $request->getPost()->pk;
            $name = $request->getPost()->name;
            $value = $request->getPost()->value;
            
            $this->getSousFamilleTable()->xeditUpdate($pk, $name, $value);
        }
    }
    
    public function deleteAction()
    {
        $id = $this->params('id');
        $this->getSousFamilleTable()->deleteSsfamille($id);
        return $this->redirect()->toRoute('foam/default', array('controller'=>'sousfamille'));
    }
    
    public function xeditableAction()
    { 
        $id_famille = $this->params('id');
        $data = $this->getSousFamilleTable()->getSsflist($id_famille);
        
        $jsonModel = new JsonModel($data);
        return $jsonModel;
    }
}